<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
// import { createRequire } from "module";
// const require = createRequire(import.meta.url);
// console.log(require, "require");

import * as echarts from "echarts";
// require("echarts/theme/macarons"); // echarts theme
import("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

let chart = null
const animationDuration = 3000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      // chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!chart) {
      return;
    }
    chart.dispose();
    chart = null;
  },
  methods: {
    initChart() {
      chart = echarts.init(this.$el, "macarons");
      chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: ['止咳宝片', '四季感冒片', '双黄连口服液', '叶酸达片', '眼药水', '碘酒']
        },
        series: [
          {
            type: 'bar',
            data: [18203, 23489, 29034, 104970, 131744, 630230]
          },
        ]
      });
    },
  },
};
</script>
